import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

public class Run {
    private static final int LISTENS_PORT = 25525;
    private static Database database = new Database();

    public static void main(String[] args) throws IOException {
        System.out.println("读取数据库中……");
        try {
            database.readFromFile();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("读取数据库成功!");
        // System.out.println(database.getAccountRecordDataMap());
        // System.out.println(database.getAccountPasswordDataMap());
        new Connect.Listens(LISTENS_PORT).start();
        Business business = new Business();
        business.start();
        Scanner keyin = new Scanner(System.in);
        while (!keyin.nextLine().equals("stop"))
            System.out.println("请输入stop关闭数据库.");
        System.out.println("数据写出中……");
        try {
            database.writeToFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("数据写出完毕!");
        keyin.close();
        System.exit(0);
    }

    static boolean is() {
        return false;
    }

    static class Business extends Thread {
        @Override
        public void run() {
            while (!Connect.getIsConnected()) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Thread.currentThread().interrupt();
                }
            }
            try {
                while (true) {
                    String in = Connect.readLine();
                    if (in.equals("<get password>")) {
                        String account = Connect.readLine();
                        AccountPassword password = database.getAccountPassword(account);
                        if (password == null) {
                            Connect.println("<<<<<<<<<<NULL>>>>>>>>>>");
                        } else {
                            Connect.println(password.getPassword());
                        }
                    } else if (in.equals("<get record>")) {
                        String account = Connect.readLine();
                        AccountRecord record = database.getAccountRecord(account);
                        if (record == null) {
                            System.err.println("查无" + account + "的战绩");
                            Connect.println("-1\n-1\n-1\n");
                        } else {
                            Connect.println(Integer.toString(record.getMoney()));
                            Connect.println(Integer.toString(record.getWin()));
                            Connect.println(Integer.toString(record.getGames()));
                        }
                    } else if (in.equals("<register>")) {
                        String account = Connect.readLine();
                        String password = Connect.readLine();
                        if (database.getAccountPasswordDataMap().containsKey(account)) {
                            Connect.println("fail");
                        } else {
                            database.putAccountPassword(account, password);
                            database.putAccountRecord(account, new AccountRecord(0, 0, 0));
                            Connect.println("succeed");
                        }
                    } else if (in.equals("<change money>")) {
                        String account = Connect.readLine();
                        int delta = Integer.parseInt(Connect.readLine());
                        database.getAccountRecord(account).changeMoney(delta);
                    } else if (in.equals("<win>")) {
                        String account = Connect.readLine();
                        database.getAccountRecord(account).win();
                    } else if (in.equals("<lose>")) {
                        String account = Connect.readLine();
                        database.getAccountRecord(account).lose();
                    } else {
                        System.err.println("未知的命令[" + in + "]");
                    }
                    if (is()) {
                        break;
                    }
                }
            } catch (

            IOException e) {
                e.printStackTrace();
            }
        }
    }
}